package com.magnificent.panda.model.generator.log;

import com.magnificent.panda.model.entity.FlowActor;
import com.magnificent.panda.model.entity.FlowEvent;
import com.magnificent.panda.model.generator.Parser;
import com.silverhaze.commons.utils.collections.list.MetaArrayList;
import com.silverhaze.commons.utils.collections.list.MetaList;
import com.silverhaze.commons.utils.other.Pair;

import java.io.File;
import java.util.Scanner;

/**
 * User: Babanin
 * Date: 05.01.11
 * Time: 5:37
 */
public class AxionAgentLogParser extends Parser {
    @Override
    public MetaList<Pair<FlowActor, FlowActor>> parseSeparately(String filename) {
        MetaList<Pair<FlowActor, FlowActor>> result = new MetaArrayList<Pair<FlowActor, FlowActor>>();

        try {
            long minTime = Long.MAX_VALUE;
            Scanner scanner = new Scanner(new File(filename));

            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                String[] strings = line.split("\t");

                if (strings.length == 3) {
                    String sender = strings[0];
                    String recipient = strings[1];
                    Long time = Long.valueOf(strings[2]);

                    minTime = Math.min(time, minTime);

                    boolean found = false;
                    for (Pair<FlowActor, FlowActor> actorPair : result) {
                        FlowActor first = actorPair.getFirst();
                        FlowActor second = actorPair.getSecond();

                        if (first.getName().equals(sender) && second.getName().equals(recipient)) {
                            first.getEvents().add(new FlowEvent(-1, time));
                            found = true;

                            break;
                        } else if (second.getName().equals(sender) && first.getName().equals(recipient)) {
                            second.getEvents().add(new FlowEvent(-1, time));
                            found = true;

                            break;
                        }
                    }

                    if (!found) {
                        FlowActor senderActor = new FlowActor(sender);
                        FlowActor recipientActor = new FlowActor(recipient);

                        senderActor.getEvents().add(new FlowEvent(-1, time));

                        senderActor.setPartner(recipientActor);
                        recipientActor.setPartner(senderActor);

                        result.add(new Pair<FlowActor, FlowActor>(senderActor, recipientActor));
                    }
                } else {
                    System.err.println("Incorrect string = " + line);
                }
            }

            for (Pair<FlowActor, FlowActor> actorPair : result) {
                for (FlowEvent event : actorPair.getFirst().getEvents()) {
                    event.setValue(event.getValue() - minTime);
                }

                for (FlowEvent event : actorPair.getSecond().getEvents()) {
                    event.setValue(event.getValue() - minTime);
                }
            }

            scanner.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }
}
